#include "uti_orderseries_mq.h"
#include "uti_interface.h"
#include "uti_consolecolor.h"

OrderSeries_Mq::OrderSeries_Mq(OrderSerInfo* pserinfo, HANDLE* phan):m_phan_callback(phan)
{

	if (!GetOrderSeries(pserinfo, &m_pser))
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[OrderSeries_Mq] GetOrderSeries Failed"<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  

		return;

	}


	m_pstart = (m_pser->m_pordercur - m_pser->m_orderindex);

	m_index = 0;

	*m_phan_callback = CreateEvent(NULL, false, false, NULL);

	m_size = m_pser->m_orderserinfo.sersize;
};

OrderSeries_Mq::OrderSeries_Mq(unsigned int sersize, void (*pcallback_fun)(), HANDLE* phan):m_phan_callback(phan)
{
	if (!GetOrderSeries(sersize, pcallback_fun, &m_pser))
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[OrderSeries_Mq] GetOrderSeries Failed"<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  

		return;

	}


	m_pstart = (m_pser->m_pordercur - m_pser->m_orderindex);

	m_index = 0;

	*m_phan_callback = CreateEvent(NULL, false, false, NULL);

	m_size = m_pser->m_orderserinfo.sersize;
}



OrderSeries_Mq::~OrderSeries_Mq()
{
	if (NULL != m_pser)
		delete m_pser;


	CloseHandle(*m_phan_callback);
};

int OrderSeries_Mq::CheckReadindex()
{
	int ret = 0;

	m_index++;

	if (m_index == m_size)
	{
		SetEvent(*m_phan_callback);

		m_index = 0;

		ret = 1;
	}

	return ret;

}